/*
 * $Id: BurningWheelRoller.java 9 2009-12-09 04:03:02Z dave.terhune $
 *
 * Copyright (C) 2009 David Terhune
 *
 * This file is part of DiceRoller.
 *
 * DiceRoller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DiceRoller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DiceRoller.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dterhune.gaming.dice;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.Random;

/** Rolls dice for Burning Wheel. */
@SuppressWarnings("serial") // Never actually serialized, so ignore warning
class BurningWheelRoller extends Random implements View.OnClickListener
{
    /** Number of items to print per line. */
    private static final int ITEMS_PER_LINE = 10;

    /** Result text field. */
    TextView result;
    /** Successes text field. */
    TextView success;
    /** Traitors text field. */
    TextView traitor;
    /** Shade radio group. */
    RadioGroup shade;
    /** Open-ended check box. */
    CheckBox openEnded;
    
    /** 
     * Creates a new BurningWheelRoller.
     * @param activity The parent DiceRoller
     */
    public BurningWheelRoller(Activity activity)
    {
        // Set the click listener
        View b = activity.findViewById(R.id.bwp1);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp2);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp3);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp4);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp5);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp6);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp7);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp8);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp9);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.bwp10);
        b.setOnClickListener(this);

        // Cache the text fields
        result = (TextView)activity.findViewById(R.id.bw_result);
        success = (TextView)activity.findViewById(R.id.bw_success);
        traitor = (TextView)activity.findViewById(R.id.bw_traitor);
        shade = (RadioGroup)activity.findViewById(R.id.bw_shade);
        openEnded = (CheckBox)activity.findViewById(R.id.bw_open);
    }

    /**
     * Click handler.
     * @param v view that was clicked
     */
    public void onClick(View v)
    {
        // Figure out how many dice to roll
        int dice = 0;
        switch(v.getId())
        {
            case R.id.bwp1:  dice =  1; break;
            case R.id.bwp2:  dice =  2; break;
            case R.id.bwp3:  dice =  3; break;
            case R.id.bwp4:  dice =  4; break;
            case R.id.bwp5:  dice =  5; break;
            case R.id.bwp6:  dice =  6; break;
            case R.id.bwp7:  dice =  7; break;
            case R.id.bwp8:  dice =  8; break;
            case R.id.bwp9:  dice =  9; break;
            case R.id.bwp10: dice = 10; break;
        }

        // Figure out the target number
        int target = 4;
        switch(shade.getCheckedRadioButtonId())
        {
            case R.id.bw_black:
                target = 4;
                break;

            case R.id.bw_grey:
                target = 3;
                break;

            case R.id.bw_white:
                target = 2;
                break;
        }

        // Open-ended?
        boolean open = openEnded.isChecked();

        int successes = 0;
        int traitors = 0;
        StringBuilder sb = new StringBuilder();

        // Roll the dice
        for(int i = 0; i < dice; i++)
        {
            // Append new line or comma
            if(i != 0 && (i % ITEMS_PER_LINE) == 0)
            {
                sb.append('\n');
            }
            else if(i != 0)
            {
                sb.append(", ");
            }
            int d = nextInt(6) + 1;
            sb.append(d);
            if(d >= target)
            {
                // This is a success
                successes++;
            }
            else
            {
                // Traitor
                traitors++;
            }
            if(open && d == 6)
            {
                // This one's a six; add another die to the pool
                dice++;
            }
        }

        // Display the results
        result.setText(sb);
        success.setText(Integer.toString(successes));
        traitor.setText(Integer.toString(traitors));
    }
}
